package com.directorio.controller;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class FrontEnd extends HttpServlet {

    private static final long serialVersionUID = 1L;
    public static ApplicationContext CONTEXT = null;

    public FrontEnd() {
        super();
    }

    public void init(ServletConfig config) {
        try {
            super.init(config);
        } catch (ServletException ex) {
            Logger.getLogger(FrontEnd.class.getName()).log(Level.SEVERE, null, ex);
        }
        requestContext();
    }
    private void requestContext() {
        System.out.println("Iniciando Contexto..!");
        if (FrontEnd.CONTEXT == null) {
            FrontEnd.CONTEXT = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        }
        System.out.println("Contexto Iniciado Correctamente...!");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        procestServlet(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        procestServlet(request, response);
    }

    private void procestServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = "";
        url += "/" + request.getParameter("localize");
        this.getServletConfig().getServletContext().getRequestDispatcher(url).forward(request, response);
    }
}
